﻿// The container contains a content control plus children controls.
// When highlighted or selected, it is the content control that has the visual indication
define([
    'Xjs/ui/View',
    'Xjs/core/util',
    'Xjs/ui/Control' // Include the content module in case the container has a content component
],
function (View, util) {
    'use strict';

    var type = 'container',
    Container = Xjs.getType(type);

    if (Container) { // It has been already loaded
        return Container;
    }

    // A container is a composite of content and a tooltip, although is a composite so it can have any children
    Container = util.define({
        base: View,

        construct: function (conf, parent) {

            Container.base.constructor.call(this, conf, parent);
        },

        type: type,
        defaultChildType: 'control',

        // Whether to include the content as one of the children
        contentIncluded: false,

        onBeforeBuild: function () {
            if (this.content) {
                this.builder.addNode(this.content.build());
            }
        },

        onRender: function () {

            if (this.content) {
                this.content.onRender();
            }

            Container.base.onRender.call(this);
        },

        createContent: function (conf) {
            this.content = Xjs.create('control', conf, this);
        },

        getContent: function () {
            return this.content;
        },

        // Populates all the content control and the children controls with declared binders from the data
        valueFrom: function (data) {

            if (this.content) {
                this.content.valueFrom(data);
            }

            if (this.children) {
                this.children.forEach(function (child) {
                    child.valueFrom(data);
                });
            }
        },

        // Populates the data from all the content control and the children controls with declared binders
        valueTo: function (data) {

            if (this.content) {
                this.content.valueTo(data);
            }

            if (this.children) {
                this.children.forEach(function (child) {
                    child.valueTo(data);
                });
            }
        }
    });

    Xjs.registerType(Container);

    return Container;
});